//
//  LoginView.m
//  基本框架
//
//  Created by runone02 on 2018/6/29.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "LoginView.h"
#import "ZMRegisterViewController.h"

@interface LoginView  ()
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIButton *closeButton;   //取消按钮

@property (nonatomic, strong) UIImageView *logoImageView;   //logo图片
@property (nonatomic, strong) UITextField *userNameField;    //用户名
@property (nonatomic, strong) UITextField *passwordField;    //密码名
@property (nonatomic, strong) YYLabel *forgetPwLabel;   //忘记密码
@property (nonatomic, strong) YYLabel *registerLabel;    //注册
@property (nonatomic, strong) UIButton *loginButton;    //登录按钮
@property (nonatomic, strong) YYLabel *closeLabel;   //取消

@property (nonatomic, strong) UILabel *bottomLine1;
@property (nonatomic, strong) UILabel *bottomLine2;
@end

@implementation LoginView

- (UIView *) mainView{
    if (!_mainView) {
        _mainView = [[UIView alloc]init];
        _mainView.backgroundColor =[ZMColor clearColor];
        [self.scrollView addSubview:_mainView];
        
        [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(0);
            make.width.mas_equalTo(self.width);
            make.height.mas_equalTo(self.height + 50);
        }];
    }
    return _mainView;
}


- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        [self addSubview:self.scrollView];
        [self insertSubview:_scrollView aboveSubview:self.bgImageView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(0);
            make.width.mas_equalTo(self.width);
            make.height.mas_equalTo(self.height);
        }];
        [_scrollView.superview layoutIfNeeded];
        _scrollView.contentSize = CGSizeMake(0, _scrollView.height + 1);
    }
    return _scrollView;
}

- (UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc]init];
        _bgImageView.image = IMAGE(@"login_bg");
        [self addSubview:self.bgImageView];
        [self sendSubviewToBack:_bgImageView];
        [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(self);
        }];
    }
    return _bgImageView;
    
}

- (UIButton *)closeButton{
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = IMAGE(@"login_alert_cancel");
        _closeButton.layer.cornerRadius = 30*0.5;
        _closeButton.layer.masksToBounds = YES;
        _closeButton.layer.borderColor = [ZMColor whiteColor].CGColor;
        _closeButton.layer.borderWidth = 1;
        [_closeButton setImage:image forState:UIControlStateNormal];
        [self addSubview:_closeButton];
        
        [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20+5);
            make.left.mas_equalTo(5);
            make.width.height.mas_equalTo(30);
        }];
        [_closeButton.superview layoutIfNeeded];
        [_closeButton addTarget:self action:@selector(clickLoginOut:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (instancetype) initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [ZMColor appGraySpaceColor];
    }
    return self;
}


- (void) setupUI{
    [self closeButton];
    [self bgImageView];
    [self mainView];
    WEAKSELF;
    self.logoImageView = [[UIImageView alloc]init];
    UIImage *logoImage = IMAGE(@"login_header");
    self.logoImageView.image = logoImage;
    [self.mainView addSubview:self.logoImageView];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(logoImage.size);
        make.centerX.mas_equalTo(self.mainView);
        make.top.mas_equalTo(CGRectGetMaxY(self.closeButton.frame)+40);
    }];
    
    self.userNameField = [[UITextField alloc]init];
    self.userNameField.font = TITLE_FONT(30);
    NSAttributedString *userNameString = [[NSAttributedString alloc]initWithString:@"用户名"attributes:@{NSForegroundColorAttributeName:[ZMColor appLinghtGrayColor],NSFontAttributeName : self.userNameField.font}];
    self.userNameField.attributedPlaceholder = userNameString;
    self.userNameField.textColor = [ZMColor whiteColor];
    self.userNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.mainView addSubview:self.userNameField];
    [self.userNameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.logoImageView.mas_bottom).with.offset(60);
    }];
    
    UIView *userNameMainView = [[UIView alloc]init];
    UIImageView *userNameView = [[UIImageView alloc]init];
    UIImage *userNameLeftImage = IMAGE(@"icon_telephone");
    userNameView.image = userNameLeftImage;
    userNameView.size = userNameLeftImage.size;
    userNameView.x = 0;
    userNameView.y = 0;
    
    userNameMainView.size = CGSizeMake(userNameLeftImage.size.width + 10, userNameLeftImage.size.height);
    [userNameMainView addSubview:userNameView];
    
    self.userNameField.leftView = userNameMainView;
    self.userNameField.leftViewMode = UITextFieldViewModeAlways;
    
    self.bottomLine1 = [[UILabel alloc]init];
    self.bottomLine1.backgroundColor = [ZMColor appLinghtGrayColor];
    [self.mainView addSubview:self.bottomLine1];
    
    [self.bottomLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(KMarginLeft);
        make.right.mas_equalTo(-KMarginLeft);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.userNameField.mas_bottom).with.offset(1);
    }];
    
    
    self.passwordField = [[UITextField alloc]init];
    self.passwordField.font = TITLE_FONT(30);
    self.passwordField.textColor = [ZMColor whiteColor];
    NSAttributedString *passwordString = [[NSAttributedString alloc]initWithString:@"密码" attributes:@{NSForegroundColorAttributeName : [ZMColor appLinghtGrayColor],NSFontAttributeName : self.userNameField.font}];
    self.passwordField.attributedPlaceholder = passwordString;
    self.passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.mainView addSubview:self.passwordField];
    
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(self.userNameField.mas_bottom).with.offset(1);
    }];
    
    UIView * passwordMainView = [[UIView alloc]init];
    UIImageView *passwordView = [[UIImageView alloc]init];
    UIImage *passwordLeftImage = IMAGE(@"icon_lock");
    passwordView.image = passwordLeftImage;
    passwordView.size = passwordLeftImage.size;
    passwordView.x = 0;
    passwordView.y = 0;
    passwordMainView.size = CGSizeMake(passwordLeftImage.size.width + 10, passwordLeftImage.size.height);
    [passwordMainView addSubview:passwordView];
    
    self.passwordField.leftView = passwordMainView;
    self.passwordField.leftViewMode = UITextFieldViewModeAlways;
    
    self.bottomLine2 = [[UILabel alloc]init];
    self.bottomLine2.backgroundColor = [ZMColor appLinghtGrayColor];
    [self.mainView addSubview:self.bottomLine2];
    [self.bottomLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(KMarginLeft);
        make.right.mas_equalTo(-KMarginLeft);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.passwordField.mas_bottom).with.offset(1);
    }];
    
    //忘记密码
    self.forgetPwLabel = [[YYLabel alloc]init];
    self.forgetPwLabel.font = TITLE_FONT(30);
    self.forgetPwLabel.text = @"忘记密码?";
    self.forgetPwLabel.textColor = [ZMColor whiteColor];
    [self.mainView addSubview: self.forgetPwLabel];
    [self.forgetPwLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userNameField.mas_left);
        make.top.mas_equalTo(self.passwordField.mas_bottom).with.offset(20);
    }];
    self.forgetPwLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSLog(@"点击了忘记密码");
    };
    
    //注册
    self.registerLabel = [[YYLabel alloc]init];
    self.registerLabel.font = TITLE_FONT(30);
    self.registerLabel.textColor = [ZMColor whiteColor];
    self.registerLabel.text = @"注册账号";
    [self.mainView addSubview:self.registerLabel];
    [self.registerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.userNameField.mas_right);
        make.top.mas_equalTo(self.passwordField.mas_bottom).with.offset(20);
    }];
    self.registerLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSLog(@"点击注册");
        ZMRegisterViewController * registerView = [[ZMRegisterViewController alloc]init];
        [weakSelf.viewController.navigationController pushViewController:registerView animated:YES];
    };
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginButton.titleLabel.font = TITLE_FONT(30);
    self.loginButton.layer.masksToBounds = YES;
    self.loginButton.layer.cornerRadius = 20;
    self.loginButton.backgroundColor = [ZMColor colorWithHexString:@"55667D"];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.mainView addSubview:self.loginButton];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(KMarginLeft);
        make.right.mas_equalTo(-KMarginLeft);
        make.top.mas_equalTo(self.forgetPwLabel.mas_bottom).with.offset(25);
        make.height.mas_equalTo(45);
    }];
    
    [self.loginButton addTarget:self action:@selector(clickLoginButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //继续浏览
    self.closeLabel = [[YYLabel alloc]init];
    self.closeLabel.font = TITLE_FONT( 30);
    self.closeLabel.textColor = [ZMColor whiteColor];
    self.closeLabel.textAlignment = NSTextAlignmentCenter;
    self.closeLabel.text = @"继续浏览";
    [self.mainView addSubview:self.closeLabel];
    
    [self.closeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mainView);
        make.top.mas_equalTo(self.loginButton.mas_bottom).with.offset(25);
    }];
    
    self.closeLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weakSelf.viewController dismissViewControllerAnimated:YES completion:^{
            
        }];
    };
}

#pragma mark - 退出页面
- (void)clickLoginOut:(UIButton *)btn{
    btn.enabled = NO;
    [self.viewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark  登录
- (void)clickLoginButton:(UIButton *)btn{
    if (![self.userNameField.text stringByTrim].length) {
        [SVProgressHUD showInfoWithStatus:@"请输入用户名"];
        return;
    }
    if (![self.passwordField.text stringByTrim].length) {
        [SVProgressHUD showInfoWithStatus:@"请输入密码"];
        return;
    }
    NSString *userName = self.userNameField.text;
    NSString*password = self.passwordField.text;
    
    WEAKSELF;
    [SVProgressHUD showWithStatus:@"正在登录...."];
    if (userName.length && password.length) {
        [AVUser logInWithUsernameInBackground:userName password:password block:^(AVUser * _Nullable user, NSError * _Nullable error) {
            [SVProgressHUD dismissWithDelay:1.0f];
            if (error) {
                NSDictionary * dic = error.userInfo;
                NSLog(@"登录失败 %@",dic[@"error"]);
                [SVProgressHUD showInfoWithStatus:@"用户名和密码不匹配"];
            }else{
                [SVProgressHUD showInfoWithStatus:@"登录成功"];
                //这里存储ToKen 下次直接登录
                [[ZMUserInfo shareUserInfo] loadUserInfo:user];
                [[ZMUserInfo shareUserInfo]saveUserInfoToSandbox];
                
                //发送登录成功通知
                [[NSNotificationCenter defaultCenter] postNotificationName:KLoginStateChangeNotice object:nil];
                [weakSelf.viewController dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }
        }];
    }
}


@end
